package com.csdj.t301.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * author:cloud
 * date:2021/3/19 0019
 **/
public class DateUtil {

    /**
     * 把字符串转化成日期
     * @param date
     * @param format
     * @return
     */
    public static Date stringToDate(String date,String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 把日期按格式转换成字符串
     * @param date
     * @param format
     * @return
     */
    public static String dateToString(Date date,String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 把yyyy-MM-dd 转换成日期
     * @param date
     * @return
     */
    public static Date toDateYMD(String date){
        return stringToDate(date,"yyyy-MM-dd");
    }

    public static void main(String[] args) {
        System.out.println(dateToString(new Date(),"yyyyMMddhhmmssSSS"));
        System.out.println(System.currentTimeMillis());
        Date da = new Date(1616120869670l);
        System.out.println(dateToString(da,"yyyyMMddhhmmssSSS"));
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH,1);
        cal.set(Calendar.YEAR,2020);
        cal.add(Calendar.DAY_OF_YEAR,-10);
        System.out.println(cal.getTime().toLocaleString());
    }
}
